package queries;

/**
 * @author KRISTHI@N
 *
 */

import implementation.Client;

import java.sql.SQLException;
import javax.swing.JOptionPane;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;



public class Insert
{
    ConnectionJDBC connetionJDBC; 
    Connection connection;
    private boolean bError = false;
    Client client;
    
    public void insertData(String consulta)
    {
        connetionJDBC = new ConnectionJDBC();
        connection = (Connection) connetionJDBC.getConexion();
        
        try 
        {
        	Statement estamentoPreparado=(PreparedStatement) connection.prepareStatement(consulta);  
        	estamentoPreparado.executeUpdate(consulta);
        } 
        catch (com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException ex) 
		{   
			bError=true;
		}
		catch (SQLException ex) 
		{   
			JOptionPane.showMessageDialog(null,"no pudo buscar");
			ex.printStackTrace();
		}
	}
    
    public boolean retornarError()
	{
		return bError;
	}

	public void createCliente(Client client) 
	{
		this.client=client;
		String sQuerie="";
		sQuerie="Insert into cliente values("+client.mGetNNit()+",'"+client.mGetSNameCliente()+"','"+client.mGetSAddress()+"',"+client.mGetNPhone()+","+client.mGetNCity()+",'"+client.mGetSNameResta()+"','"+client.mGetSInfoAditional()+"')";
		System.out.println(sQuerie);
		insertData(sQuerie);
	}
	
	public int insertInsumos(String consulta) 
	{
		connetionJDBC = new ConnectionJDBC();
        connection = (Connection) connetionJDBC.getConexion();
        int codigo=0;
		try 
		{
			System.out.println("entre a actualizar"+consulta);
			PreparedStatement estamentoPreparado=(PreparedStatement) connection.prepareStatement(consulta);  
			ResultSet resultado = null;
			resultado = (ResultSet) estamentoPreparado.executeQuery();
			while(resultado.next())
			{
				codigo =(resultado.getInt(1));
			}
		} 
		catch (SQLException ex) 
		{   
			JOptionPane.showMessageDialog(null,"no pudo buscar por Autor");
			ex.printStackTrace();
		}
		
		return codigo;
	}
}
